//
//  ShouDongDianHuoCell.m
//  DriverClient
//
//  Created by 吕晶 on 2020/8/28.
//  Copyright © 2020 吕晶. All rights reserved.
//

#import "ShouDongDianHuoCell.h"

@interface ShouDongDianHuoCell ()

@property (nonatomic, strong) UIView *bgView1;
@property (nonatomic, strong) UITextField *goodField;
@property (nonatomic, strong) UILabel *line1;
@property (nonatomic, strong) UIButton *goodBtn;
@property (nonatomic, strong) UITextField *countField;
@property (nonatomic, strong) UILabel *line2;
@property (nonatomic, strong) UITextField *pieceField;

@property (nonatomic, strong) UIButton *luRuBtn;

@end

@implementation ShouDongDianHuoCell

+ (instancetype)ShouDongDianHuoCellWithTableView:(UITableView *)tableView {
    static NSString *cellID = @"ShouDongDianHuoCell";
    ShouDongDianHuoCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell == nil) {
        cell = [[ShouDongDianHuoCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    return cell;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.backgroundColor = Color_backgroundColor;
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        self.bgView1 = [UIView new];
        self.bgView1.backgroundColor = HexColor(0xffffff);
        self.bgView1.layer.masksToBounds = YES;
        [self.contentView addSubview:self.bgView1];
        
        self.goodField = [UITextField new];
        self.goodField.textColor = HexColor(0x2B2C44);
        self.goodField.font = [UIFont systemFontOfSize:16];
        self.goodField.textAlignment = NSTextAlignmentRight;
        self.goodField.placeholder = @"名称/编号";
        [self.bgView1 addSubview:self.goodField];
        
        self.line1 = [UILabel new];
        self.line1.backgroundColor = Color_backgroundColor;
        [self.bgView1 addSubview:self.line1];
        
        self.goodBtn = [UIButton new];
        [self.goodBtn addTarget:self action:@selector(goodClick) forControlEvents:UIControlEventTouchUpInside];
        [self.bgView1 addSubview:self.goodBtn];
        
        self.countField = [UITextField new];
        self.countField.textColor = HexColor(0x2B2C44);
        self.countField.font = [UIFont systemFontOfSize:16];
        self.countField.textAlignment = NSTextAlignmentRight;
        self.countField.placeholder = @"请输入";
        self.countField.keyboardType = UIKeyboardTypeDecimalPad;
        [self.countField addTarget:self action:@selector(countFieldChange:) forControlEvents:UIControlEventEditingChanged];
        [self.bgView1 addSubview:self.countField];
        
        self.line2 = [UILabel new];
        self.line2.backgroundColor = Color_backgroundColor;
        [self.bgView1 addSubview:self.line2];
        
        self.pieceField = [UITextField new];
        self.pieceField.textColor = HexColor(0x2B2C44);
        self.pieceField.font = [UIFont systemFontOfSize:16];
        self.pieceField.textAlignment = NSTextAlignmentRight;
        self.pieceField.placeholder = @"请输入";
        self.pieceField.keyboardType = UIKeyboardTypeDecimalPad;
        [self.pieceField addTarget:self action:@selector(pieceFieldChange:) forControlEvents:UIControlEventEditingChanged];
        [self.bgView1 addSubview:self.pieceField];
        
        self.luRuBtn = [UIButton new];
        self.luRuBtn.backgroundColor = Color_ThemeColor;
        [self.luRuBtn setTitle:@"确认录入" forState:0];
        [self.luRuBtn setTitleColor:HexColor(0xffffff) forState:0];
        self.luRuBtn.titleLabel.font = BoldFontOfSize(16);
        self.luRuBtn.layer.masksToBounds = YES;
        self.luRuBtn.layer.cornerRadius = 6;
        [self.luRuBtn addTarget:self action:@selector(luRuClick) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:self.luRuBtn];
        
    }
    return self;
}

- (void)countFieldChange:(UITextField *)field {
    if (self.countBlock) {
        self.countBlock(field.text);
    }
}

- (void)pieceFieldChange:(UITextField *)field {
    if (self.pieceBlock) {
        self.pieceBlock(field.text);
    }
}

- (void)goodClick {
    if (self.goodClickBlock) {
        self.goodClickBlock();
    }
}

-(void)setModel:(ZhuangCheMainGoodModel *)model {
    _model = model;
    [self configUI];
}

- (void)configUI {
    self.bgView1.nim_left = 15;
    self.bgView1.nim_top = 10;
    self.bgView1.nim_width = ScreenW - 30;
    self.bgView1.nim_height = 182;
    self.bgView1.layer.cornerRadius = 6;
    
    self.goodField.frame = CGRectMake(15, 0, self.bgView1.nim_width - 30, 60);
    UILabel *goodTitleLab = [[UILabel alloc] initWithFrame:CGRectMake(0, 21.5, 70, 17)];
    goodTitleLab.text = @"选择货物";
    goodTitleLab.font = [UIFont systemFontOfSize:16];
    goodTitleLab.textColor = HexColor(0x2B2C44);
    self.goodField.leftView = goodTitleLab;
    self.goodField.leftViewMode = UITextFieldViewModeAlways;
    UIImageView *goodArrowImgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 21, 18, 18)];
    goodArrowImgView.image = [UIImage imageNamed:@"icon_lower"];
    self.goodField.rightView = goodArrowImgView;
    self.goodField.rightViewMode = UITextFieldViewModeAlways;
    
    if (self.model.name.length) {
        self.goodField.text = self.model.name;
    }
    
    self.line1.frame = CGRectMake(15, self.goodField.nim_bottom, self.goodField.nim_width, 1);
    self.goodBtn.frame = self.goodField.frame;
    
    self.countField.frame = CGRectMake(self.goodField.nim_left, self.line1.nim_bottom, self.goodField.nim_width, self.goodField.nim_height);
    UIView *countLeftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 80, self.countField.nim_height)];
    UILabel *xing1Lab = [UILabel new];
    xing1Lab.text = @"*";
    xing1Lab.textColor = HexColor(0xF35959);
    xing1Lab.font = [UIFont systemFontOfSize:16];
    [xing1Lab sizeToFit];
    xing1Lab.nim_left = 0;
    xing1Lab.nim_top = 21.5;
    [countLeftView addSubview:xing1Lab];
    UILabel *countTitleLab = [[UILabel alloc] initWithFrame:CGRectMake(xing1Lab.nim_right + 5, xing1Lab.nim_top, countLeftView.nim_width - (xing1Lab.nim_right + 5), 17)];
    countTitleLab.text = @"货物数量";
    countTitleLab.font = [UIFont systemFontOfSize:16];
    countTitleLab.textColor = HexColor(0x2B2C44);
    [countLeftView addSubview:countTitleLab];
    self.countField.leftView = countLeftView;
    self.countField.leftViewMode = UITextFieldViewModeAlways;
    
    if (self.model.z_count) {
        self.countField.text = [NSString stringWithFormat:@"%d",self.model.z_count];
    }
    
    self.line2.frame = CGRectMake(self.countField.nim_left, self.countField.nim_bottom, self.countField.nim_width, 1);
    
    self.pieceField.frame = CGRectMake(self.countField.nim_left, self.line2.nim_bottom, self.countField.nim_width, self.countField.nim_height);
    UIView *pieceLeftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 80, self.pieceField.nim_height)];
    UILabel *xing2Lab = [UILabel new];
    xing2Lab.text = @"*";
    xing2Lab.textColor = HexColor(0xF35959);
    xing2Lab.font = [UIFont systemFontOfSize:16];
    [xing2Lab sizeToFit];
    xing2Lab.nim_left = 0;
    xing2Lab.nim_top = 21.5;
    [pieceLeftView addSubview:xing2Lab];
    UILabel *pieceTitleLab = [[UILabel alloc] initWithFrame:CGRectMake(xing2Lab.nim_right + 5, xing2Lab.nim_top, pieceLeftView.nim_width - (xing2Lab.nim_right + 5), 17)];
    pieceTitleLab.text = @"件数";
    pieceTitleLab.font = [UIFont systemFontOfSize:16];
    pieceTitleLab.textColor = HexColor(0x2B2C44);
    [pieceLeftView addSubview:pieceTitleLab];
    self.pieceField.leftView = pieceLeftView;
    self.pieceField.leftViewMode = UITextFieldViewModeAlways;
    
    if (self.model.z_piece) {
        self.pieceField.text = [NSString stringWithFormat:@"%d",self.model.z_piece];
    }
    
    self.luRuBtn.frame = CGRectMake(15, self.bgView1.nim_bottom + 20, ScreenW - 30, 44);
    self.height = self.luRuBtn.nim_bottom + 20;
}

- (void)luRuClick {
    if (self.luRuBlock) {
        self.luRuBlock();
    }
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
